
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.MemberBillDao;
import com.sun.showcase.biz.mall.service.MemberBillService;
import com.sun.showcase.biz.mall.service.MemberService;
import com.sun.showcase.biz.mall.service.OrderService;
import com.sun.showcase.biz.mall.service.PlatformConfigService;
import com.sun.showcase.client.domain.mall.MemberBill;
import com.sun.showcase.client.domain.mall.Order;
import com.sun.showcase.client.domain.mall.PlatformConfig;
import com.sun.showcase.client.query.mall.MemberBillQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.DateUtil;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("memberBillService")
@Transactional
public class MemberBillServiceImpl implements MemberBillService{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MemberBillDao memberBillDao;
	@Autowired
	private PlatformConfigService platformConfigService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private MemberService memberService;
	
	public void setMemberBillDao(MemberBillDao dao) {
		this.memberBillDao = dao;
	}

	public DataGrid datagrid(MemberBillQuery memberBillQuery) {
		DataGrid j = new DataGrid();
		Pager<MemberBill> pager  = find(memberBillQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<MemberBillQuery> getQuerysFromEntitys(List<MemberBill> memberBills) {
		List<MemberBillQuery> memberBillQuerys = new ArrayList<MemberBillQuery>();
		if (memberBills != null && memberBills.size() > 0) {
			for (MemberBill tb : memberBills) {
				MemberBillQuery b = new MemberBillQuery();
				BeanUtils.copyProperties(tb, b);
				memberBillQuerys.add(b);
			}
		}
		return memberBillQuerys;
	}

	private Pager<MemberBill> find(MemberBillQuery memberBillQuery) {
		Page<MemberBill> page = PageHelper.startPage(memberBillQuery.getPage().intValue(), memberBillQuery.getRows().intValue());
		List<MemberBill> list = memberBillDao.findList(memberBillQuery);
		Pager<MemberBill> pager = new Pager<MemberBill>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public MemberBill add(MemberBillQuery memberBillQuery) {
		MemberBill t = new MemberBill();
		BeanUtils.copyProperties(memberBillQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		memberBillDao.save(t);
		memberBillQuery.setId(t.getId());
		return t;
		
	}

	public void update(MemberBillQuery memberBillQuery) {
		MemberBill t = memberBillDao.getById(memberBillQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(memberBillQuery, t);
	    	memberBillDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(MemberBillQuery memberBillQuery) {
	    memberBillDao.updatePart(memberBillQuery);
	}

	public void delete(String[] ids) {
		memberBillDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		memberBillDao.deletePt(ids);
	}

	public MemberBill get(MemberBillQuery memberBillQuery) {
		return memberBillDao.getById(memberBillQuery.getId());
	}

	public MemberBill get(String id) {
		return memberBillDao.getById(id);
	}

	
	public List<MemberBillQuery> listAll(MemberBillQuery memberBillQuery) {
	    List<MemberBill> list = memberBillDao.findList(memberBillQuery);
		List<MemberBillQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void dealBillJob() {
		PlatformConfig conf = platformConfigService.get();
		MemberBillQuery query = new MemberBillQuery();
		query.setStatus("0");
		query.setType("0");
		this.dealBill(1, query, conf);
	}
	
	private void dealBill(int startPage,MemberBillQuery query,PlatformConfig conf) {
		Page<MemberBill> page = PageHelper.startPage(startPage, 100);
		List<MemberBill> list = memberBillDao.findList(query);
		for(MemberBill mb : list) {
			Order order = orderService.get(mb.getBussinessId());
			if(order == null) {
				continue;
			}
			//过了订单可退款日期
			if(conf != null && order.getOrderCompleteDate() !=null && DateUtil.getDaySub(order.getOrderCompleteDate(), new Date())>=conf.getOrderReturnDay()) {
				//更新状态
				MemberBillQuery up = new MemberBillQuery();
				up.getSearchMap().put(StringConstant.ID, mb.getId());
				up.setStatus("1");
				this.updatePart(up);
				//更新余额
				memberService.updateBalance(mb.getUserId(), mb.getAmount(), mb.getAmount()*-1);
			}
			
		}
		if(startPage < page.getPageNum()) {
			//查询处理下一页
			startPage++;
			this.dealBill(startPage, query,conf);
		}
	}
	
	@Override
	public void updateStatus(String bid, String status) {
		MemberBillQuery up = new MemberBillQuery();
		up.setStatus(status);
		up.getSearchMap().put("bussiness_id", bid);
		this.updatePart(up);
	}
	
}
